package com.bango.blackberry;

import java.util.Date;
import java.util.TimeZone;

import net.rim.device.api.servicebook.ServiceBook;
import net.rim.device.api.servicebook.ServiceRecord;
import net.rim.device.api.system.Branding;
import net.rim.device.api.system.CoverageInfo;
import net.rim.device.api.system.DeviceInfo;
import net.rim.device.api.system.RadioInfo;
import net.rim.device.api.system.WLANInfo;

public class BangoStatic
{

	public static Date GetUTCDate()
	{
		return new Date(System.currentTimeMillis() + TimeZone.getDefault().getRawOffset() * -1);
	}

	public static String ConnectionType(int connectionType)
	{
		String returnValue = "OTHER";

		switch (connectionType)
		{
			case BangoConstants.WIFI_CONNECTION:
				returnValue = "WIFI";
				break;
			case RadioInfo.NETWORK_GPRS:
			case RadioInfo.NETWORK_UMTS:
			case RadioInfo.NETWORK_CDMA:
			case RadioInfo.NETWORK_802_11:
			case RadioInfo.NETWORK_IDEN:
				returnValue = "OPERATOR";
				break;
		}

		return returnValue;
	}

	public static String OperatorConnectionType(int operatorConnectionType)
	{
		String returnValue = "OTHER";
		switch (operatorConnectionType)
		{
			case RadioInfo.NETWORK_GPRS:
				returnValue = "GPRS";
				break;
			case RadioInfo.NETWORK_UMTS:
				returnValue = "UMTS";
				break;
			case RadioInfo.NETWORK_CDMA:
				returnValue = "CDMA";
				break;
		}
		return returnValue;
	}

	public static String getUserAgent()
	{
		String userAgent = "Blackberry" + DeviceInfo.getDeviceName() + "/" + DeviceInfo.getSoftwareVersion() + " Profile/" + System.getProperty("microedition.profiles") + " Configuration/" + System.getProperty("microedition.configuration") + " VendorID/" + Branding.getVendorId();
		return userAgent;

	}

	public static String getConnectionToUse()
    {
        String returnValue = null;

       if(WLANInfo.getWLANState() == WLANInfo.WLAN_STATE_CONNECTED)
        {
    	   returnValue = ";interface=wifi";
        }
        else if((CoverageInfo.getCoverageStatus() & CoverageInfo.COVERAGE_DIRECT) == CoverageInfo.COVERAGE_DIRECT)
        {
            String carrierUid = getCarrierBIBSUid();
            if(carrierUid == null)
            {
            	returnValue = ";deviceside=true";
            }
            else
            {
            	returnValue = ";deviceside=false;connectionUID="+carrierUid + ";ConnectionType=mds-public";
            }
        }
        else if((CoverageInfo.getCoverageStatus() & CoverageInfo.COVERAGE_MDS) == CoverageInfo.COVERAGE_MDS)
        {
        	returnValue = ";deviceside=false";
        }
        else if(CoverageInfo.getCoverageStatus() == CoverageInfo.COVERAGE_NONE)
        {
        }
        else
        {
        	returnValue = ";deviceside=true";
        }

        return returnValue;
    }

    /**
     * Looks through the phone's service book for a carrier provided BIBS network
     * @return The uid used to connect to that network.
     */
    private static String getCarrierBIBSUid()
    {
        ServiceRecord[] records = ServiceBook.getSB().getRecords();
        int currentRecord;

        for(currentRecord = 0; currentRecord < records.length; currentRecord++)
        {
            if(records[currentRecord].getCid().toLowerCase().equals("ippp"))
            {
                if(records[currentRecord].getName().toLowerCase().indexOf("bibs") >= 0)
                {
                    return records[currentRecord].getUid();
                }
            }
        }

        return null;
    }


}
